// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core.logger;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipError;
import zombie.core.Core;
import zombie.debug.DebugLog;
import zombie.network.MD5Checksum;

public final class ZipLogs {
    static ArrayList<String> filePaths = new ArrayList();

    public static void addZipFile(boolean saveTheSaveData) {
        FileSystem fileSystem = null;

        try {
            String string0 = zombie.ZomboidFileSystem.instance.getCacheDir() + File.separator + "logs.zip";
            String string1 = new File(string0).toURI().toString();
            URI uRI = URI.create("jar:" + string1);
            Path path = FileSystems.getDefault().getPath(string0).toAbsolutePath();
            HashMap hashMap = new HashMap();
            hashMap.put("create", String.valueOf(Files.notExists(path, new LinkOption[0])));

            try {
                fileSystem = FileSystems.newFileSystem(uRI, hashMap);
            } catch (IOException iOException0) {
                iOException0.printStackTrace();
                return;
            } catch (ZipError zipError) {
                zipError.printStackTrace();
                DebugLog.log("Deleting possibly-corrupt " + string0);

                try {
                    Files.deleteIfExists(path);
                } catch (IOException iOException1) {
                    iOException1.printStackTrace();
                }

                return;
            }

            long long0 = getMD5FromZip(fileSystem, "/meta/console.txt.md5");
            long long1 = getMD5FromZip(fileSystem, "/meta/coop-console.txt.md5");
            long long2 = getMD5FromZip(fileSystem, "/meta/server-console.txt.md5");
            long long3 = getMD5FromZip(fileSystem, "/meta/DebugLog.txt.md5");
            addLogToZip(fileSystem, "console", "console.txt", long0);
            addLogToZip(fileSystem, "coop-console", "coop-console.txt", long1);
            addLogToZip(fileSystem, "server-console", "server-console.txt", long2);
            addDebugLogToZip(fileSystem, "debug-log", "DebugLog.txt", long3);
            addToZip(fileSystem, "/configs/options.ini", "options.ini");
            addToZip(fileSystem, "/configs/popman-options.ini", "popman-options.ini");
            addToZip(fileSystem, "/configs/latestSave.ini", "latestSave.ini");
            addToZip(fileSystem, "/configs/debug-options.ini", "debug-options.ini");
            addToZip(fileSystem, "/configs/sounds.ini", "sounds.ini");
            addToZip(fileSystem, "/addition/translationProblems.txt", "translationProblems.txt");
            addToZip(fileSystem, "/addition/gamepadBinding.config", "gamepadBinding.config");
            addFilelistToZip(fileSystem, "/addition/mods.txt", "mods");
            addDirToZipLua(fileSystem, "/lua", "Lua");
            addDirToZip(fileSystem, "/db", "db");
            addDirToZip(fileSystem, "/server", "Server");
            addDirToZip(fileSystem, "/statistic", "Statistic");
            if (!saveTheSaveData) {
                addSaveOldToZip(fileSystem, "/save_old/map_t.bin", "map_t.bin");
                addSaveOldToZip(fileSystem, "/save_old/map_ver.bin", "map_ver.bin");
                addSaveOldToZip(fileSystem, "/save_old/map.bin", "map.bin");
                addSaveOldToZip(fileSystem, "/save_old/map_sand.bin", "map_sand.bin");
                addSaveOldToZip(fileSystem, "/save_old/reanimated.bin", "reanimated.bin");
                addSaveOldToZip(fileSystem, "/save_old/zombies.ini", "zombies.ini");
                addSaveOldToZip(fileSystem, "/save_old/z_outfits.bin", "z_outfits.bin");
                addSaveOldToZip(fileSystem, "/save_old/map_p.bin", "map_p.bin");
                addSaveOldToZip(fileSystem, "/save_old/map_meta.bin", "map_meta.bin");
                addSaveOldToZip(fileSystem, "/save_old/map_zone.bin", "map_zone.bin");
                addSaveOldToZip(fileSystem, "/save_old/serverid.dat", "serverid.dat");
                addSaveOldToZip(fileSystem, "/save_old/thumb.png", "thumb.png");
                addSaveOldToZip(fileSystem, "/save_old/players.db", "players.db");
                addSaveOldToZip(fileSystem, "/save_old/players.db-journal", "players.db-journal");
                addSaveOldToZip(fileSystem, "/save_old/vehicles.db", "vehicles.db");
                addSaveOldToZip(fileSystem, "/save_old/vehicles.db-journal", "vehicles.db-journal");
                putTextFile(fileSystem, "/save_old/description.txt", getLastSaveDescription());
            } else {
                addSaveToZip(fileSystem, "/save/map_t.bin", "map_t.bin");
                addSaveToZip(fileSystem, "/save/map_ver.bin", "map_ver.bin");
                addSaveToZip(fileSystem, "/save/map.bin", "map.bin");
                addSaveToZip(fileSystem, "/save/map_sand.bin", "map_sand.bin");
                addSaveToZip(fileSystem, "/save/reanimated.bin", "reanimated.bin");
                addSaveToZip(fileSystem, "/save/zombies.ini", "zombies.ini");
                addSaveToZip(fileSystem, "/save/z_outfits.bin", "z_outfits.bin");
                addSaveToZip(fileSystem, "/save/map_p.bin", "map_p.bin");
                addSaveToZip(fileSystem, "/save/map_meta.bin", "map_meta.bin");
                addSaveToZip(fileSystem, "/save/map_zone.bin", "map_zone.bin");
                addSaveToZip(fileSystem, "/save/serverid.dat", "serverid.dat");
                addSaveToZip(fileSystem, "/save/thumb.png", "thumb.png");
                addSaveToZip(fileSystem, "/save/players.db", "players.db");
                addSaveToZip(fileSystem, "/save/players.db-journal", "players.db-journal");
                addSaveToZip(fileSystem, "/save/vehicles.db", "vehicles.db");
                addSaveToZip(fileSystem, "/save/vehicles.db-journal", "vehicles.db-journal");
                putTextFile(fileSystem, "/save/description.txt", getCurrentSaveDescription());
            }

            try {
                fileSystem.close();
            } catch (IOException iOException2) {
                iOException2.printStackTrace();
            }
        } catch (Exception exception) {
            if (fileSystem != null) {
                try {
                    fileSystem.close();
                } catch (IOException iOException3) {
                    iOException3.printStackTrace();
                }
            }

            exception.printStackTrace();
        }
    }

    private static void copyToZip(Path path1, Path path2, Path path3) throws IOException {
        Path path0 = path1.resolve(path2.relativize(path3).toString());
        if (Files.isDirectory(path3, new LinkOption[0])) {
            Files.createDirectories(path0);
        } else {
            Files.copy(path3, path0);
        }
    }

    public static void addToZip(FileSystem zipfs, String filenameInternal, String filename) {
        try {
            Path path0 = zipfs.getPath(filenameInternal);
            Files.createDirectories(path0.getParent());
            Path path1 = FileSystems.getDefault().getPath(zombie.ZomboidFileSystem.instance.getCacheDir() + File.separator + filename).toAbsolutePath();
            Files.deleteIfExists(path0);
            if (Files.exists(path1, new LinkOption[0])) {
                Files.copy(path1, path0, StandardCopyOption.REPLACE_EXISTING);
            }
        } catch (IOException iOException) {
            iOException.printStackTrace();
        }
    }

    private static void addSaveToZip(FileSystem fileSystem, String string0, String string1) {
        try {
            Path path0 = fileSystem.getPath(string0);
            Files.createDirectories(path0.getParent());
            Path path1 = FileSystems.getDefault().getPath(zombie.ZomboidFileSystem.instance.getFileNameInCurrentSave(string1)).toAbsolutePath();
            Files.deleteIfExists(path0);
            if (Files.exists(path1, new LinkOption[0])) {
                Files.copy(path1, path0, StandardCopyOption.REPLACE_EXISTING);
            }
        } catch (IOException iOException) {
            iOException.printStackTrace();
        }
    }

    private static void addSaveOldToZip(FileSystem fileSystem, String string2, String string3) {
        try {
            BufferedReader bufferedReader = null;

            try {
                bufferedReader = new BufferedReader(
                    new FileReader(new File(zombie.ZomboidFileSystem.instance.getCacheDir() + File.separator + "latestSave.ini"))
                );
            } catch (FileNotFoundException fileNotFoundException) {
                return;
            }

            String string0 = bufferedReader.readLine();
            String string1 = bufferedReader.readLine();
            bufferedReader.close();
            Path path0 = fileSystem.getPath(string2);
            Files.createDirectories(path0.getParent());
            Path path1 = FileSystems.getDefault()
                .getPath(zombie.ZomboidFileSystem.instance.getSaveDir() + File.separator + string1 + File.separator + string0 + File.separator + string3)
                .toAbsolutePath();
            Files.deleteIfExists(path0);
            if (Files.exists(path1, new LinkOption[0])) {
                Files.copy(path1, path0, StandardCopyOption.REPLACE_EXISTING);
            }
        } catch (IOException iOException) {
            iOException.printStackTrace();
        }
    }

    private static String getLastSaveDescription() {
        try {
            BufferedReader bufferedReader = null;

            try {
                bufferedReader = new BufferedReader(
                    new FileReader(new File(zombie.ZomboidFileSystem.instance.getCacheDir() + File.separator + "latestSave.ini"))
                );
            } catch (FileNotFoundException fileNotFoundException) {
                return "-";
            }

            String string0 = bufferedReader.readLine();
            String string1 = bufferedReader.readLine();
            bufferedReader.close();
            return "World: " + string0 + "\n\rGameMode:" + string1;
        } catch (IOException iOException) {
            iOException.printStackTrace();
            return "-";
        }
    }

    private static String getCurrentSaveDescription() {
        String string0 = "Sandbox";
        if (Core.GameMode != null) {
            string0 = Core.GameMode;
        }

        String string1 = "-";
        if (Core.GameSaveWorld != null) {
            string1 = Core.GameSaveWorld;
        }

        return "World: " + string1 + "\n\rGameMode:" + string0;
    }

    public static void addDirToZip(FileSystem zipfs, String filenameInternal, String filename) {
        try {
            Path path0 = zipfs.getPath(filenameInternal);
            deleteDirectory(zipfs, path0);
            Files.createDirectories(path0);
            Path path1 = FileSystems.getDefault().getPath(zombie.ZomboidFileSystem.instance.getCacheDir() + File.separator + filename).toAbsolutePath();
            Stream stream = Files.walk(path1);
            stream.forEach(path2 -> {
                try {
                    copyToZip(path0, path1, path2);
                } catch (IOException iOExceptionx) {
                    throw new RuntimeException(iOExceptionx);
                }
            });
        } catch (IOException iOException) {
        }
    }

    private static void addDirToZipLua(FileSystem fileSystem, String string0, String string1) {
        try {
            Path path0 = fileSystem.getPath(string0);
            deleteDirectory(fileSystem, path0);
            Files.createDirectories(path0);
            Path path1 = FileSystems.getDefault().getPath(zombie.ZomboidFileSystem.instance.getCacheDir() + File.separator + string1).toAbsolutePath();
            Stream stream = Files.walk(path1);
            stream.forEach(path0x -> {
                try {
                    if (!path0x.endsWith("ServerList.txt") && !path0x.endsWith("ServerListSteam.txt")) {
                        copyToZip(path0, path1, path0x);
                    }
                } catch (IOException iOExceptionx) {
                    throw new RuntimeException(iOExceptionx);
                }
            });
        } catch (IOException iOException) {
        }
    }

    private static void addFilelistToZip(FileSystem fileSystem, String string0, String string1) {
        try {
            Path path0 = fileSystem.getPath(string0);
            Path path1 = FileSystems.getDefault().getPath(zombie.ZomboidFileSystem.instance.getCacheDir() + File.separator + string1).toAbsolutePath();
            Stream stream = Files.list(path1);
            String string2 = (String)stream.map(Path::getFileName).map(Path::toString).collect(Collectors.joining("; "));
            Files.deleteIfExists(path0);
            Files.write(path0, string2.getBytes(), new OpenOption[0]);
        } catch (IOException iOException) {
        }
    }

    static void deleteDirectory(FileSystem fileSystem, Path path) {
        filePaths.clear();
        getDirectoryFiles(path);

        for (String string : filePaths) {
            try {
                Files.delete(fileSystem.getPath(string));
            } catch (IOException iOException) {
                iOException.printStackTrace();
            }
        }
    }

    static void getDirectoryFiles(Path path) {
        try {
            Stream stream = Files.walk(path);
            stream.forEach(path1 -> {
                if (!path1.toString().equals(path.toString())) {
                    if (Files.isDirectory(path1, new LinkOption[0])) {
                        getDirectoryFiles(path1);
                    } else if (!filePaths.contains(path1.toString())) {
                        filePaths.add(path1.toString());
                    }
                }
            });
            filePaths.add(path.toString());
        } catch (IOException iOException) {
        }
    }

    private static void addLogToZip(FileSystem fileSystem, String string1, String string0, long long1) {
        long long0;
        try {
            long0 = MD5Checksum.createChecksum(zombie.ZomboidFileSystem.instance.getCacheDir() + File.separator + string0);
        } catch (Exception exception0) {
            long0 = 0L;
        }

        File file = new File(zombie.ZomboidFileSystem.instance.getCacheDir() + File.separator + string0);
        if (file.exists() && !file.isDirectory() && long0 != long1) {
            try {
                Path path0 = fileSystem.getPath("/" + string1 + "/log_5.txt");
                Files.delete(path0);
            } catch (Exception exception1) {
            }

            for (int _int = 5; _int > 0; _int--) {
                Path path1 = fileSystem.getPath("/" + string1 + "/log_" + _int + ".txt");
                Path path2 = fileSystem.getPath("/" + string1 + "/log_" + (_int + 1) + ".txt");

                try {
                    Files.move(path1, path2);
                } catch (Exception exception2) {
                }
            }

            try {
                Path path3 = fileSystem.getPath("/" + string1 + "/log_1.txt");
                Files.createDirectories(path3.getParent());
                Path path4 = FileSystems.getDefault().getPath(zombie.ZomboidFileSystem.instance.getCacheDir() + File.separator + string0).toAbsolutePath();
                Files.copy(path4, path3, StandardCopyOption.REPLACE_EXISTING);
                Path path5 = fileSystem.getPath("/meta/" + string0 + ".md5");
                Files.createDirectories(path5.getParent());

                try {
                    Files.delete(path5);
                } catch (Exception exception3) {
                }

                Files.write(path5, String.valueOf(long0).getBytes(), new OpenOption[0]);
            } catch (Exception exception4) {
                exception4.printStackTrace();
            }
        }
    }

    private static void addDebugLogToZip(FileSystem fileSystem, String string3, String string4, long long1) {
        String string0 = null;
        File file0 = new File(LoggerManager.getLogsDir());
        String[] string1 = file0.list();

        for (int int0 = 0; int0 < string1.length; int0++) {
            String string2 = string1[int0];
            if (string2.contains("DebugLog.txt")) {
                string0 = LoggerManager.getLogsDir() + File.separator + string2;
                break;
            }
        }

        if (string0 != null) {
            long long0;
            try {
                long0 = MD5Checksum.createChecksum(string0);
            } catch (Exception exception0) {
                long0 = 0L;
            }

            File file1 = new File(string0);
            if (file1.exists() && !file1.isDirectory() && long0 != long1) {
                try {
                    Path path0 = fileSystem.getPath("/" + string3 + "/log_5.txt");
                    Files.delete(path0);
                } catch (Exception exception1) {
                }

                for (int int1 = 5; int1 > 0; int1--) {
                    Path path1 = fileSystem.getPath("/" + string3 + "/log_" + int1 + ".txt");
                    Path path2 = fileSystem.getPath("/" + string3 + "/log_" + (int1 + 1) + ".txt");

                    try {
                        Files.move(path1, path2);
                    } catch (Exception exception2) {
                    }
                }

                try {
                    Path path3 = fileSystem.getPath("/" + string3 + "/log_1.txt");
                    Files.createDirectories(path3.getParent());
                    Path path4 = FileSystems.getDefault().getPath(string0).toAbsolutePath();
                    Files.copy(path4, path3, StandardCopyOption.REPLACE_EXISTING);
                    Path path5 = fileSystem.getPath("/meta/" + string4 + ".md5");
                    Files.createDirectories(path5.getParent());

                    try {
                        Files.delete(path5);
                    } catch (Exception exception3) {
                    }

                    Files.write(path5, String.valueOf(long0).getBytes(), new OpenOption[0]);
                } catch (Exception exception4) {
                    exception4.printStackTrace();
                }
            }
        }
    }

    private static long getMD5FromZip(FileSystem fileSystem, String string) {
        long _long = 0L;

        try {
            Path path = fileSystem.getPath(string);
            if (Files.exists(path, new LinkOption[0])) {
                List list = Files.readAllLines(path);
                _long = Long.parseLong((String)list.get(0));
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }

        return _long;
    }

    public static void putTextFile(FileSystem zipfs, String filename, String text) {
        try {
            Path path = zipfs.getPath(filename);
            Files.createDirectories(path.getParent());

            try {
                Files.delete(path);
            } catch (Exception exception0) {
            }

            Files.write(path, text.getBytes(), new OpenOption[0]);
        } catch (Exception exception1) {
            exception1.printStackTrace();
        }
    }
}
